﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Permissions
{
    /// <summary>
    /// 电脑信息
    /// </summary>
    public class ComputerInfor
    {
        /// <summary>
        /// 机器CPU信息
        /// </summary>
        public string Name, Description, Manufacturer, ProcessorId, NumberOfCores, NumberOfLogicalProcessors;
        /// <summary>
        /// 机器序列号
        /// </summary>
        public string UUID;
        public ComputerInfor() { }

        public ComputerInfor(string Name, string Description, string Manufacturer,
            string ProcessorId, string NumberOfCores, string NumberOfLogicalProcessors, string MAC, string UUID)
        {
            this.Name = Name;
            this.Description = Description;
            this.Manufacturer = Manufacturer;
            this.ProcessorId = ProcessorId;
            this.NumberOfCores = NumberOfCores;
            this.NumberOfLogicalProcessors = NumberOfLogicalProcessors;
            this.UUID = UUID;
        }

        public string GetInfor()
        {
            return this.Name + this.Description + this.Manufacturer + this.ProcessorId + this.NumberOfCores + this.NumberOfLogicalProcessors
                + this.UUID;
        }

        /// <summary>
        /// SHA256加密
        /// </summary>
        /// <param name="rawData"></param>
        /// <returns></returns>
        public string ComputeSHA256()
        {
            using (SHA256 sha256 = SHA256.Create())
            {
                byte[] inputBytes = Encoding.ASCII.GetBytes(GetInfor());
                byte[] hashBytes = sha256.ComputeHash(inputBytes);

                // Convert the byte array to a hexadecimal string.
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < hashBytes.Length; i++)
                {
                    sb.Append(hashBytes[i].ToString("X2"));
                }
                return sb.ToString();
            }
        }

        /// <summary>
        /// 解密方法
        /// </summary>
        /// <param name="ciphertext"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentException"></exception>
        public byte[] Decrypt(byte[] ciphertext, byte[] key, byte[] iv)
        {
            if (ciphertext == null || ciphertext.Length <= 0)
                throw new ArgumentNullException(nameof(ciphertext));
            if (key == null || key.Length != 8)
                throw new ArgumentException("DES密钥必须是8字节长。", nameof(key));
            if (iv == null || iv.Length != 8)
                throw new ArgumentException("IV必须是8字节长，或者与算法要求的块大小相匹配。", nameof(iv));

            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
            {
                des.Key = key;
                des.IV = iv;
                des.Mode = CipherMode.CBC;
                des.Padding = PaddingMode.PKCS7;

                ICryptoTransform decryptor = des.CreateDecryptor(des.Key, des.IV);

                using (MemoryStream msDecrypt = new MemoryStream(ciphertext))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                        {
                            return Encoding.UTF8.GetBytes(srDecrypt.ReadToEnd());
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 加密方法
        /// </summary>
        /// <param name="plaintext"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentException"></exception>
        public byte[] Encrypt(byte[] plaintext, byte[] key, byte[] iv)
        {
            if (plaintext == null || plaintext.Length <= 0)
                throw new ArgumentNullException(nameof(plaintext));
            if (key == null || key.Length != 8) // DES密钥必须是8字节长
                throw new ArgumentException("DES密钥必须是8字节长。", nameof(key));
            if (iv == null || iv.Length != 8) // DES的块大小是8字节，因此IV也通常是8字节（尽管不是严格要求的）
                throw new ArgumentException("IV必须是8字节长，或者与算法要求的块大小相匹配。", nameof(iv));

            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
            {
                des.Key = key;
                des.IV = iv;
                des.Mode = CipherMode.CBC; // 通常使用CBC模式
                des.Padding = PaddingMode.PKCS7; // PKCS7填充是常见的选择

                ICryptoTransform encryptor = des.CreateEncryptor(des.Key, des.IV);

                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                        {
                            swEncrypt.Write(Encoding.UTF8.GetString(plaintext));
                        }
                        return msEncrypt.ToArray();
                    }
                }
            }
        }

        // 辅助方法：将字节数组转换为字符串（Base64编码）
        public string ByteArrayToString(byte[] ba)
        {
            return Convert.ToBase64String(ba);
        }
        // 辅助方法：将字符串（Base64编码）转换为字节数组
        public byte[] StringToByteArray(string hex)
        {
            return Convert.FromBase64String(hex);
        }
    }
}
